package com.ixuestar.controller;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ixuestar.service.SmartTerminalService;
import com.ixuestar.util.ReturnResult;

@CrossOrigin
@RestController
@RequestMapping(value = "/smartTerminal",method = RequestMethod.POST)
public class SmartTerminalController extends BaseController{
	
	@Autowired
	private SmartTerminalService smartTerminalService;
	
//	//创建返回对象
//	@Autowired
//	private ReturnResult ReturnResult = new ReturnResult();
	
	/**
	 * 用户终端登陆
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getLoginup")
	@ResponseBody
	public Map<String, Object> getLoginup(@RequestBody JSONObject jsonObject,HttpServletResponse response){
//		this.setCrossHeader(response);
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		Map<String, Object> result = null;
		try {
			JSONObject loginup = smartTerminalService.getLoginup(jsonObject);
			//判断参数
			Integer integer = loginup.getInteger("code");
			if (integer==0) {
				loginup.remove("code");
				result = ReturnResult.correctResult(loginup);
			}else if (integer==1) {
				result = ReturnResult.otherResult(integer, "用户名有误，请重新输入");
			}else if (integer==2) {
				result = ReturnResult.otherResult(integer, "密码有误，请重新输入");
			}else if (integer==3) {
				result = ReturnResult.otherResult(integer, "用户未绑定个人信息，请绑定后使用");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = ReturnResult.wrongResult();
		}
		return result;
	}
	
	/**
	 * 激活并修改密码
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/updatePassword")
	@ResponseBody
	public Map<String, Object> updatePassword(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		Map<String, Object> map = null;
		try {
			smartTerminalService.updatePassword(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(jsonObject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 绑定会员卡和学生关系
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/writeMemberStudent")
	@ResponseBody
	public Map<String, Object> writeMemberStudent(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//首先判断非空参数
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		//创建返回参数集合
		Map<String, Object> map = null;
		try {
			smartTerminalService.writeMemberStudent(jsonObject);
			//返回
			map = ReturnResult.correctResult(new JSONObject());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 获取学校列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getSchool")
	@ResponseBody
	public Map<String, Object> getSchool(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolList = smartTerminalService.getSchoolList(jsonObject);
			//返回正确结果
			map = ReturnResult.correctResult(schoolList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 获取学校年级
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getSchoolGrade")
	@ResponseBody
	public Map<String, Object> getSchoolGrade(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolGradeList = smartTerminalService.getSchoolGradeList(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(schoolGradeList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 获取年级下的班级
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getSchoolClass")
	@ResponseBody
	public Map<String, Object> getSchoolClass(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolClassList = smartTerminalService.getSchoolClassList(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(schoolClassList);
		} catch (Exception e) { 
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 获取班级的学生信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getSchoolStudent")
	@ResponseBody
	public Map<String, Object> getSchoolStudent(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject schoolStudentList = smartTerminalService.getSchoolStudentList(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(schoolStudentList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 充值卡充值
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/useRechargeCard")
	@ResponseBody
	public Map<String, Object> useRechargeCard(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject jjObject = smartTerminalService.useRechargeCard(jsonObject);
			int useRechargeCard = jjObject.getInteger("code");
			JSONObject topupcardInfo = jjObject.getJSONObject("topupcardInfo");
			if (useRechargeCard==0) {
				//正确返回
				map = ReturnResult.correctResult(topupcardInfo);
			}else if (useRechargeCard==1) {
				map = ReturnResult.otherResult(useRechargeCard, "会员号有误，请核实后在充值");
			}else if (useRechargeCard==2) {
				map = ReturnResult.otherResult(useRechargeCard, "充值卡验证码无效，请核实后充值");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 添加实名信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/writeRealnameInfo")
	@ResponseBody
	public Map<String, Object> writeRealnameInfo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		Map<String, Object> map = null;
		try {
			smartTerminalService.writeRealnameInfo(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(new JSONObject());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 获取爱学豆
	 * 
	 */
	@RequestMapping("/getIxuestarb")
	@ResponseBody
	public Map<String, Object> getIxuestarb(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		if (jsonObject.isEmpty()) this.validateObjectNullException(null, "参数不能为空");
		
		Map<String, Object> map = null;
		try {
			System.out.println(jsonObject);
			JSONObject ixuestarDou = smartTerminalService.getIxuestarDou(jsonObject);
			//返回
			map = ReturnResult.correctResult(ixuestarDou);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * 打印扣豆接口
	 * 
	 */
	@RequestMapping("/deductIxuestar")
	@ResponseBody
	public Map<String, Object> deductIxuestar(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject jsonObject2 = smartTerminalService.deductIxuestar(jsonObject);
			Integer integer = jsonObject2.getInteger("code");
			if (integer==0) {
				//正确返回
				jsonObject2.remove("code");
				map = ReturnResult.correctResult(jsonObject2);
			}else {
				map = ReturnResult.otherResult(integer, "爱学豆不足，请及时充值");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	
	/**
	 * 充值获取用户信息接口
	 * 
	 */
	@RequestMapping("/getIxuestarStudentInfo")
	@ResponseBody
	public Map<String, Object> getIxuestarStudentInfo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		Map<String, Object> map = null;
		try {
			String ixmNo = jsonObject.getString("ixmNo");
			//获取学生个人信息
			JSONObject memberStudentInfo = smartTerminalService.getMemberStudentInfo(ixmNo);
			if (memberStudentInfo.isEmpty()) {
				map = ReturnResult.otherResult(1,"对不起，您输入的会员卡号不存在，请核对后输入");
			}else {
				map = ReturnResult.correctResult(memberStudentInfo);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 写入用户打印记录
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/writeMemberPrintRecord")
	@ResponseBody
	public Map<String, Object> writeMemberPrintRecord(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject writeMemberPrintRecord = smartTerminalService.writeMemberPrintRecord(jsonObject);
			//正确返回
			map = ReturnResult.correctResult(writeMemberPrintRecord);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取用户打印记录
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMemberPrintRecords")
	@ResponseBody
	public Map<String, Object> getMemberPrintRecords(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		Map<String, Object> map = null;
		try {
			JSONObject memberPrintRecords = smartTerminalService.getMemberPrintRecords(jsonObject);
			int integer = memberPrintRecords.getInteger("code");
			if (integer==0) {
				map = ReturnResult.correctResult(memberPrintRecords);
			}else {
				//正确返回
				map = ReturnResult.otherResult(integer, "用户已打印过此文件");				
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
}
